package com.ruoyi.Unit.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.Course.service.impl.StudyCourseServiceImpl;
import com.ruoyi.Goover.domain.StudyEnglish;
import com.ruoyi.Goover.mapper.StudyEnglishMapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Unit.domain.StudyUnit;
import com.ruoyi.Unit.service.IStudyUnitService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 存储单元Controller
 * 
 * @author luobom
 * @date 2021-12-03
 */
@RestController
@RequestMapping("/Unit/Unit")
public class StudyUnitController extends BaseController
{
    @Autowired
    private IStudyUnitService studyUnitService;

    @Autowired
    StudyCourseServiceImpl studyCourseService ;
    @Autowired
    private StudyEnglishMapper studyEnglishMapper;

    /**
     * 查询存储单元列表
     */
    @PreAuthorize("@ss.hasPermi('Unit:Unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudyUnit studyUnit)
    {
        startPage();
        studyUnit.setCourseuid(studyCourseService.GetDefaultCourse().getUid());
        List<StudyUnit> list = studyUnitService.selectStudyUnitList(studyUnit);
        return getDataTable(list);
    }

    /**
     * 导出存储单元列表
     */
    @PreAuthorize("@ss.hasPermi('Unit:Unit:export')")
    @Log(title = "存储单元", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudyUnit studyUnit)
    {
        List<StudyUnit> list = studyUnitService.selectStudyUnitList(studyUnit);
        ExcelUtil<StudyUnit> util = new ExcelUtil<StudyUnit>(StudyUnit.class);
        util.exportExcel(response, list, "存储单元数据");
    }

    /**
     * 获取存储单元详细信息
     */
    @PreAuthorize("@ss.hasPermi('Unit:Unit:query')")
    @GetMapping(value = "/{uid}")
    public AjaxResult getInfo(@PathVariable("uid") String uid)
    {
        return AjaxResult.success(studyUnitService.selectStudyUnitByUid(uid));
    }

    /**
     * 新增存储单元
     */
    @PreAuthorize("@ss.hasPermi('Unit:Unit:add')")
    @Log(title = "存储单元", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudyUnit studyUnit)
    {
        return toAjax(studyUnitService.insertStudyUnit(studyUnit));
    }

    /**
     * 修改存储单元
     */
    @PreAuthorize("@ss.hasPermi('Unit:Unit:edit')")
    @Log(title = "存储单元", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudyUnit studyUnit)
    {
        return toAjax(studyUnitService.updateStudyUnit(studyUnit));
    }

    /**
     * 删除存储单元
     */
    @PreAuthorize("@ss.hasPermi('Unit:Unit:remove')")
    @Log(title = "存储单元", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uids}")
    public AjaxResult remove(@PathVariable String[] uids)
    {
        StudyEnglish sd;
        for(String uid:uids){
            //单词
            sd=new StudyEnglish();
            sd.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            sd.setUnit(uid);
            if(studyEnglishMapper.selectStudyEnglishList(sd).size()>0){
                return toAjax(0);
            }
        }
        return toAjax(studyUnitService.deleteStudyUnitByUids(uids));
    }
}
